package cn.jekun.shop.web.service;

import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.DateJsonValueProcessor;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.McMember;
import cn.jekun.component.core.service.gen.McMemberService;
import cn.jekun.shop.app.bean.ShopMessage;
import cn.jekun.shop.common.service.ShopBaseService;
/**
 * 
 * @author twj
 *
 */
@Service(ShopDetailService.BEAN_NAME)
public class ShopDetailService extends ShopBaseService{
	public static final String BEAN_NAME = "cn.jekun.shop.web.service.ShopDetailService";
	
	@Resource(name = McMemberService.BEAN_NAME)
	McMemberService memberService;
	
	@Override
	public String getNamespace() {
		return "SHOP_SHOPMESSAGE";
	}
	/**
	 * 通过shopId加载商户信息
	 * @param shopId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public JSONObject load(String userId,@ErpShop.AnnoShopId String shopId) {
		JSONObject jsonObject = new JSONObject();
		 Map formMap = new HashMap();
		if (StringUtils.isBlank(shopId)) {
			throw new BusinessException(Constants.ERRORS.VALIDATE_ERROR_CODE,
					"商户编码不能为空");
		}
		formMap.put(ErpShop.COL_SHOP_ID, shopId);
		SqlUtis.prepareSql(formMap, "", SqlUtis.getSQL(formMap, "a."
				+ ErpShop.COL_SHOP_ID, Types.VARCHAR,SqlUtis.EQ));
		
		ShopMessage Shop = this.one(this.getStatementName("getShopInfo"),formMap);
		
		
		formMap.clear();
		if(StringUtils.isNotBlank(userId)){
			formMap.put(McMember.COL_SHOP_ID, shopId);
			formMap.put(McMember.COL_USER_ID, userId);
			formMap.put(McMember.COL_CONCERN, "1");
			SqlUtis.prepareSql(formMap, 
					SqlUtis.getSQL(formMap, McMember.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),
					SqlUtis.getSQL(formMap, McMember.COL_CONCERN,Types.VARCHAR,SqlUtis.EQ)
					,SqlUtis.getSQL(formMap, McMember.COL_USER_ID,Types.VARCHAR,SqlUtis.EQ));
			if(this.memberService.exists(formMap)){
				Shop.setMemberFlag("1");
			}else{
				Shop.setMemberFlag("0");
			}
		}
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONObject json =  JSONObject.fromObject(Shop, jsonConfig);
		jsonObject.put("shop", json);
		
		
	
		return executeRerutn(jsonObject);
	}

}
